package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.Customer;
import service.imp.CustomerManagerImp;
import check.imp.CheckImp;

/**
 * Servlet implementation class UpdateServlet
 */
public class UpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private List<String> list;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String info;
		CustomerManagerImp cmi = new CustomerManagerImp();
		CheckImp ci = new CheckImp();
		int id=Integer.parseInt(request.getParameter("id"));
		String cname=request.getParameter("cname");
		double sal=ci.checkSal(request.getParameter("sal"));
		Date birth=ci.checkBirth(request.getParameter("birth"));
		String sex=request.getParameter("sex");
		String basket= request.getParameter("basket");
		String foot = request.getParameter("foot"); 
		System.out.println(cname);
		System.out.println(birth);
		System.out.println(sex);
		System.out.println(sal);
		System.out.println(basket);
		System.out.println(foot);
		if(sal==-1){
			info = "注册失败！该月薪输入格式错误！";
			request.getSession().setAttribute("info", info);
			response.sendRedirect("error.jsp");
		}else if (birth==null) {
			info = "注册失败！该生日输入格式错误！";
			request.getSession().setAttribute("info", info);
			response.sendRedirect("error.jsp");
		}else{
			Customer customer = null;
			list = new ArrayList<String>();
			if(basket!=null)
				list.add(basket);
			if(foot!=null)
				list.add(foot);
			String[] str = null;
			if(list!=null)
				 str = new String[list.size()];
			list.toArray(str);
			if("false".equals(sex)){
				customer = new Customer(id, cname, sal, false, birth, str);
			}else{
				customer = new Customer(id, cname, sal, true, birth, str);
			}
			info = "更新成功！！";
			cmi.updateCustomer(customer);
			List<Customer> customers = cmi.fiandAllCustomer();
			request.getSession().setAttribute("customers", customers);
			response.sendRedirect("show.jsp");
		}
		System.out.println(info);
	}
}
